import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Creared with IntelliJ IDEA.
 * Description:
 * User:yxd
 * Date:2022-03-18
 * Time:16:39
 */
public class TestjbdcSelect {
    public static void main(String[] args) throws SQLException {
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("123456");
        //连接
        Connection connection = dataSource.getConnection();

        //sql语句
        String sql = "select * from student";

        //构建
        PreparedStatement statement = connection.prepareStatement(sql);
        System.out.println(statement);

        //执行
        ResultSet resultSet= statement.executeQuery();
        while(resultSet.next()){//按表里面有的内容一列一列的输出 这里的while表示行数
            String name = resultSet.getString("name");
            int age = resultSet.getInt("age");
            String id = resultSet.getString("id");
            System.out.println("name " + name + " age " + age + " id " + id);
        }
        //回收
        resultSet.close();
        statement.close();
        connection.close();


    }
}
